<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * -----------------------------------------------------
 *
 * manage area
 *
 * @package     manage
 * @subpackage  
 * @category    Value -  产品地区管理
 * @author      ChenFu
 * @since       2015-6-17 version 1.0
 * -----------------------------------------------------    
 */
class Area extends CI_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	  	$this->load->library('form_validation');   // 表单验证类
		$this->load->model('attribute_model');     // 加载产品属性模型
		$this->load->model('product_model');       // 加载产品模型 
		$this->load->model('region_model');          // 加载地区模型 
		$this->load->model('area_model');          // 加载地区模型 
		check_login(); // 检查是否登录
	}


	/**
	 * [index 地区列表]
	 * @return [type] [description]
	 */
	public function index(){
		if($_GET['pid']){
			$total = $this->attribute_model->get_num_rows($_GET['pid']);
			if($total != 0){
				// 加载分页类
				$arr = array('page_total_num' => $total,'every_page_num'=>10,'page_url_name'=>'page_id');
				$this->load->library('page',$arr);
				$data['pager']	  = $this->page->pages_all();
				$data['arr'] 	  = $this->attribute_model->get_all_data_and_page($this->page->limit,$_GET['pid']);
			}else{
				$data['error'] = '该产品暂未添加属性,<a href="'.base_url('manage/attribute/insert').'">点击添加产品属性!</a>';
			}
		}	
		$this->load->view('admin/area/index',$data);
	}

	/**
	 * [insert 增加 属性值]
	 * @return [type] [description]
	 */
	public function insert(){
		if(empty($_GET['pid']) || empty($_GET['aid'])){ // 判断参数的合理性
			$this->sys_message->show_error('参数不正确,返回重新添加');
		}
		$attr = $this->attribute_model->get_a_data_by_aid($_GET['aid']);// 根据产品属性id查询属性信息

		$data['attr'] = $attr;
		if($attr['attr_region'] == 1){ // 地区
			$province = $this->region_model->get_datelist_option(1);
			$value    = $this->value_model->get_region_by_pid_and_aid($_GET['pid'],$_GET['aid']); // 获取属性值
			if($value){
				foreach ($value as $key => $val) {
					$value[$key]['region'] = $this->region_model->get_region_name_by_id($val['reg_pid']).'-'.$this->region_model->get_region_name_by_id($val['reg_cid']).'-'.$this->region_model->get_region_name_by_id($val['reg_did']); 
				}
			}
			$this->load->view('admin/value/insert_region',compact("attr","province","value"));
		}else{
			
			if($attr['attr_type'] == 'radio' || $attr['attr_type'] == 'select'){
				$data['arr'] = $this->value_model->get_value_by_id($_GET['aid']);
			}else{
				$data['value'] = $this->value_model->get_a_value_by_id($_GET['aid']);
			}
			$this->load->view('admin/area/insert',$data);
		}
	}

	/**
	 * [insert_region 增加区域价格]
	 * @return [type] [description]
	 */
	public function insert_region(){
		$_insert = $this->input->post();
		if($this->value_model->insert_region($_insert)){
			die('ok');
		}else{
			die('no');
		}
	}


	/**
	 * [add 增加属性值]
	 */
	public function add(){
		$_insert = $this->input->post();
		if($this->value_model->insert_value($_insert)){
			die('ok');
		}else{
			die('no');
		}
	}

	/**
	 * [region_delete 删除数据]
	 * @return [type] [description]
	 */
	public function region_delete(){
		if(!empty($_GET['id']) && intval($_GET['id'])){
			if($this->value_model->region_delete($_GET['id'])){
				die('ok');
			}else{
				die('no');
			}
		}else{
			die('error_1');
		}
	}

	public function value_delete(){
		if(!empty($_GET['id']) && intval($_GET['id'])){
			if($this->value_model->value_delete($_GET['id'])){
				die('ok');
			}else{
				die('no');
			}
		}else{
			die('error_1');
		}
	}

	/**
	 * [update_region 修改]
	 * @return [type] [description]
	 */
	public function update_region(){
		$_update = $this->input->post();
		if($this->value_model->update_region($_update,$_GET['id'])){
			exit('ok');
		}else{
			exit('no');
		}
	}

	public function update_value(){
		$_update = $this->input->post();
		if($this->value_model->update_value($_update,$_GET['id'])){
			exit('ok');
		}else{
			exit('no');
		}
	}
}